<template>
	<el-select :disabled="isReadOnly" filterable :style="style" class="textBoxSelect" size="small" @change="onItemChange"
	 v-model="value" :clearable="isClearable" :multiple="isMultiple" :name="field.vModel">
		<el-option v-for="(item,  index) in data" :label="item[textName]" :value="item[valueName]"></el-option>
	</el-select>
</template>

<script>
	export default {
		name: "f2bpmui-select",
		//configtype:非必传，主要是为了方便页面直接配置configtype的值
		//sqlkey:非必传，主要是为了方便页面直接配置sqlkey的值
		//field为字符串时，则认为它是字段名
		//props,其它属性,parm:{}
		props: ['field', 'fieldvalue', 'configtype', "sqlkey", "props"],
		watch: {
			fieldvalue: {
				handler(newVal, oldVal) {
					if (newVal != this.value && newVal != oldVal && oldVal != undefined) {
						this.value = newVal;
					}
				},
				deep: true,
				immediate: false
			}
		},
		created: function() {
			var that = this;
			if (that.fieldvalue != undefined && that.fieldvalue != null) {
				that.value = that.fieldvalue.toString();
			}
			var props = undefined;
			if (that.props && typeof that.props == 'string') {
				props = eval("(" + that.props + ")")
			} else if (that.props && typeof that.props == 'object') {
				props = that.props;
			}
			if (props && props.class) {
				that.class = props.class;
			}
			if (props && props.style) {
				that.style = props.style;
			}
			that.theprops = props;
			that.initailFieldOption();
			that.initail();
		},
		data: function() {
			return {
				theprops: null,
				style: "",
				class: "textBoxShort",
				isReadOnly: false,
				textName: 'label',
				valueName: 'value',
				isMultiple: false,
				isClearable: true,
				value: '',
				sqlkeyArray: [{
					dataField: 'data',
					sqlkey: '',
					parm: null,
					flag: null,
					change: null
				}],
				configTypeArray: [{
					dataField: 'data',
					configType: '',
					flag: ''
				}],
				type: '',
				data: []
			}
		},
		methods: {
			initailFieldOption() {
				var that = this;
				if (typeof that.field == "string") {
					return false;
				}
				if (that.field.itemRender.props) {
					if (that.field.itemRender.props.isMultiple != undefined) {
						that.isMultiple = that.field.itemRender.props.isMultiple
					}
					if (that.field.itemRender.props.isReadOnly != undefined) {
						that.isReadOnly = that.field.itemRender.props.isReadOnly
					}
					if (that.field.itemRender.props.isClearable != undefined) {
						that.isClearable = that.field.itemRender.props.isClearable
					}
				}
			},
			initail() {
				var that = this;
				if (typeof that.field == "string") {
					if (that.configtype) {
						var configType = that.configtype;
						that.configTypeArray[0].configType = configType;
						that.textName = 'text';
						if (that.theprops && that.theprops.parm) {
							that.configTypeArray[0].parm = that.theprops.parm;
						}
						FUI.Form.initailConfigTypeComboBox(that);
					} else if (that.sqlkey) {
						that.sqlkeyArray[0].sqlkey = that.sqlkey;
						that.textName = 'text';
						if (that.theprops && that.theprops.parm) {
							that.sqlkeyArray[0].parm = that.theprops.parm;
						}
						FUI.Form.initailSqlKeyComboBox(that);
					}
					return false;
				}
				var theSqlKey = "";
				if (that.field.itemRender.props && that.field.itemRender.props.sqlkey) {
					theSqlKey = that.field.itemRender.props.sqlkey;
				}
				if (that.field.itemRender && that.field.itemRender.options && that.field.itemRender.options.length > 0) {
					//指定数据
					if (that.field.itemRender.props && that.field.itemRender.props.textName) {
						that.textName = that.field.itemRender.props.textName;
					}
					that.data = that.field.itemRender.options;
				} else if (theSqlKey) {
					var keyStr = that.field.itemRender.props.sqlkey;
					var parmObj = {};
					if (keyStr.indexOf('|') > -1) {
						var arr = keyStr.split('|');
						keyStr = arr[0];
						parmObj = eval('(' + arr[1].replace('parm:', '') + ')');
					}
					var key = keyStr.toString().substr(15);
					parmObj.code = key;
					var textField = 'text';
					var valueField = 'value';
					that.textName = 'text';
					var myData = Object.toAjaxJson(FUI.Handlers.FuiFrameworkHandler, 'getTextValueDataJsonByDataServiceCode', parmObj);
					var data = [];
					$.each(myData, function(i, item) {
						var v = item[valueField];
						var t = item[textField];
						var op = {
							value: v,
							label: t
						};
						data.push(op);
					});
					that.data = data;
				} else if (that.field.itemRender.props.configtype) {
					var configType = that.field.itemRender.props.configtype;
					that.configTypeArray[0].configType = configType;
					that.textName = 'text';
					FUI.Form.initailConfigTypeComboBox(that);
				}
			},
			onItemChange(value) {
				var that = this;
				var fieldName = "";
				if (typeof that.field == "string") {
					fieldName = that.field;
				} else {
					fieldName = that.field.field;
				}
				this.$emit('updatevalue', fieldName, value)
			}
		}
	};
</script>
